package com.yiren.dbaa.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.yiren.dbaa.R;
import com.yiren.dbaa.constant.G;
import com.yiren.dbaa.net.bean.WiringConfigBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author lwb
 * @date :2022/1/18 15:42
 * @description: 接线配置适配器
 */
public class WiringConfigAdapter extends BaseAdapter {

    private Context context;
    private List<WiringConfigBean> wiringConfigList;
    private OnSignalActionListener listener;
    private boolean isNewLinePort = false; //是否是免破新的接线口

    public WiringConfigAdapter(Context context, List<WiringConfigBean> wiringConfigList, OnSignalActionListener listener) {
        this.context = context;
        this.wiringConfigList = wiringConfigList;
        this.listener = listener;
        if (null != G.connectDeviceMessage) {
            String versionName = G.connectDeviceMessage.getVersionDescribe();
            if (versionName.contains("MP2.0") || versionName.contains("MPQD") || versionName.contains("MP-YR") || versionName.contains("MP-XQ") || versionName.contains("MP-WP")) {
                isNewLinePort = true;
            }
        }
    }

    @Override
    public int getCount() {
        return wiringConfigList.size();
    }

    @Override
    public Object getItem(int position) {
        return wiringConfigList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder mHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_wiring_config, parent, false);
            mHolder = new ViewHolder(convertView);
            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }
        if (isNewLinePort) {
            String lineName = "";
            switch (wiringConfigList.get(position).getLineName()) {
                case "棕线":
                    lineName = "针脚10";
                    break;
                case "灰线":
                    lineName = "针脚9";
                    break;
                case "绿线":
                    lineName = "针脚8";
                    break;
                case "黄线":
                    lineName = "针脚7";
                    break;
                case "蓝线":
                    lineName = "针脚6";
                    break;
                case "紫线":
                    lineName = "针脚5";
                    break;
                case "橙线":
                    lineName = "针脚4";
                    break;
                case "粉线":
                    lineName = "针脚3";
                    break;
                case "白线":
                    lineName = "针脚2";
                    break;
                case "黑线":
                    lineName = "针脚1";
                    break;
            }
            mHolder.tvLineName.setText(lineName);
        } else {
            mHolder.tvLineName.setText(wiringConfigList.get(position).getLineName());
        }
        mHolder.tvLineSignal.setText(wiringConfigList.get(position).getAction() == 0 ? "空" : wiringConfigList.get(position).getCarSignalEnum().getDescription());
        mHolder.tvLineAction.setText(wiringConfigList.get(position).getAction() == 0 ? "选取信号" : "取消关联");
        mHolder.tvLineAction.setOnClickListener(v -> listener.onSignalAction(position));
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.tv_line_name)
        TextView tvLineName;
        @BindView(R.id.tv_line_signal)
        TextView tvLineSignal;
        @BindView(R.id.tv_line_action)
        TextView tvLineAction;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public interface OnSignalActionListener {
        void onSignalAction(int position);
    }
}
